import pymysql
from fastapi import FastAPI,Form,HTTPException
from starlette.requests import Request
from starlette.responses import RedirectResponse
from starlette.templating import Jinja2Templates

app=FastAPI()
#用户注册
@app.post("/register")
def register(name:str=Form(...),password:str=Form(...)):
    #非空校验
    if name=="" or name is None:
        raise HTTPException(400,"用户名不能为空")
    if password=="" or password is None:
        raise HTTPException(400,"密码不能为空")
    #链接数据库
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    #获取操作对象
    cursor=conn.cursor()
    #在注册之前必须先根据用户名查找用户信息，如果查到了证明该用户已存在，无法重复注册，否则证明之前没有注册，可以正常注册
    cursor.execute("select * from user where name=%s",(name,))
    user=cursor.fetchone()
    #判断user是否为None
    if user is None:
        #可以正常注册
        cursor.execute("insert into user(name,password,status) values(%s,%s,'正常')",(name,password))
        conn.commit()
        conn.close()
        return {"msg":"注册成功"}
    else:
        raise HTTPException(400,"该用户已被注册了，请重新注册")


#用户登录页面
@app.post("/login")
def login(name:str=Form(...),password:str=Form(...)):
    if name=="" or name is None:
        raise HTTPException(400,"用户名不能为空")
    if password=="" or password is None:
        raise HTTPException(400,"密码不能为空")
    #思路：1.连接数据库  2.根据用户名和密码查询用户信息  3.如果查询到了证明登录成功，否则登录失败
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    cursor=conn.cursor()
    cursor.execute("select * from user where name=%s and password=%s",(name,password))
    user=cursor.fetchone()
    #如果查询的user为空证明用户名或密码错误，登录失败，否则登录成功
    if user is None:
        raise HTTPException(400,"用户名或密码错误，登录失败")
    else:
        conn.close()
        return {"msg":"登录成功"}

#条件查询：根据用户名或用户编码进行查询
@app.post("/select")
def select(id:str=Form(...),name:str=Form(...)):
    #连接数据库
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    cursor=conn.cursor()
    cursor.execute("select * from user where id=%s or name=%s",(id,name))
    user=cursor.fetchone()
    conn.close()
    if user is None:
        raise HTTPException(400,"查无此人")
    else:
        return {"msg":user}


tem=Jinja2Templates("mydir")
#全查用户信息
@app.get("/list")
def list(request:Request):
    #链接数据库
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    cursor=conn.cursor()
    cursor.execute("select * from user")
    users=cursor.fetchall()
    conn.close()
    return tem.TemplateResponse("list.html",{"request":request,"users":users})


#修改用户状态
#禁用用户
@app.get("/jinyong/{id}")
def jinyong(id:int):
    #连接数据库
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    cursor=conn.cursor()
    cursor.execute("update user set status='禁用' where id=%s",(id,))
    conn.commit()
    conn.close()
    #重定向到全查页面
    return RedirectResponse("/list")

@app.get("/zhengchang/{id}")
def zhengchang(id:int):
    #连接数据库
    conn=pymysql.Connection(user="root",password="yantengfei",database="2307a")
    cursor=conn.cursor()
    cursor.execute("update user set status='正常' where id=%s",(id,))
    conn.commit()
    conn.close()
    return RedirectResponse("/list")